//
//  SLMP4Parser+Minf.m
//  SLVideoParser
//
//  Created by S_LINE on 2020/6/19.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLMP4Parser+Minf.h"

#import "SLParserUtil.h"

@implementation SLMP4Parser (Minf)

- (NSDictionary *)vmhdParser:(NSData *)data
{
    if (data.length < 1)
    {
        return nil;
    }
    NSString *versionKey = @"version";
    NSString *flagsKey = @"flags";
    NSString *graphicsModeKey = @"graphicsMode";
    NSString *opcolorKey = @"opcolor";
    NSMutableDictionary *dict = [NSMutableDictionary new];
    NSData *versionData = [data subdataWithRange:NSMakeRange(0, 1)];
    uint64_t version = [SLParserUtil hexToDecimalWithData:versionData];
    [dict setObject:[NSNumber numberWithUnsignedLongLong:version] forKey:versionKey];
    uint64_t offset = 1;
    
    if (version == 0)
    {
        uint64_t flagsSize = 3;
        NSData *flagsData = [data subdataWithRange:NSMakeRange(offset, flagsSize)];
        uint64_t flags = [SLParserUtil hexToDecimalWithData:flagsData];
        offset += flagsSize;
        // graphics mode 视频合成模式，为0时拷贝原始图像，否则与opcolor进行合成
        uint64_t graphicsModeSize = 2;
        NSData *graphicsModeData = [data subdataWithRange:NSMakeRange(offset, graphicsModeSize)];
        uint64_t graphicsMode = [SLParserUtil hexToDecimalWithData:graphicsModeData];
        offset += graphicsModeSize;
        // opcolor r, g, b
        uint64_t redSize = 2;
        NSData *redData = [data subdataWithRange:NSMakeRange(offset, redSize)];
        uint64_t red = [SLParserUtil hexToDecimalWithData:redData];
        offset += redSize;
        uint64_t greenSize = 2;
        NSData *greenData = [data subdataWithRange:NSMakeRange(offset, greenSize)];
        uint64_t green = [SLParserUtil hexToDecimalWithData:greenData];
        offset += greenSize;
        uint64_t blueSize = 2;
        NSData *blueData = [data subdataWithRange:NSMakeRange(offset, blueSize)];
        uint64_t blue = [SLParserUtil hexToDecimalWithData:blueData];
        offset += blueSize;
        
        [dict setObject:[NSNumber numberWithUnsignedLongLong:flags] forKey:flagsKey];
        [dict setObject:[NSNumber numberWithUnsignedLongLong:graphicsMode] forKey:graphicsModeKey];
        [dict setObject:[NSString stringWithFormat:@"{%llu, %llu, %llu}", red, green, blue] forKey:opcolorKey];
    }
    
    return [dict copy];
}

- (NSDictionary *)smhdParser:(NSData *)data
{
    if (data.length < 1)
    {
        return nil;
    }
    NSString *versionKey = @"version";
    NSString *flagsKey = @"flags";
    NSString *balanceKey = @"balance";
    NSMutableDictionary *dict = [NSMutableDictionary new];
    NSData *versionData = [data subdataWithRange:NSMakeRange(0, 1)];
    uint64_t version = [SLParserUtil hexToDecimalWithData:versionData];
    [dict setObject:[NSNumber numberWithUnsignedLongLong:version] forKey:versionKey];
    uint64_t offset = 1;
    
    if (version == 0)
    {
        uint64_t flagsSize = 3;
        NSData *flagsData = [data subdataWithRange:NSMakeRange(offset, flagsSize)];
        uint64_t flags = [SLParserUtil hexToDecimalWithData:flagsData];
        offset += flagsSize;
        // balance 立体声平衡，[8.8] 格式值，一般为0，-1.0表示全部左声道，1.0表示全部右声道
        uint64_t leftBalanceSize = 2;
        NSData *leftBalanceData = [data subdataWithRange:NSMakeRange(offset, leftBalanceSize)];
        uint64_t leftBalance = [SLParserUtil hexToDecimalWithData:leftBalanceData];
        offset += leftBalanceSize;
        uint64_t rightBalanceSize = 2;
        NSData *rightBalanceData = [data subdataWithRange:NSMakeRange(offset, rightBalanceSize)];
        uint64_t rightBalance = [SLParserUtil hexToDecimalWithData:rightBalanceData];
        offset += rightBalanceSize;
        // reserved 保留位
        uint64_t reservedSize = 2;
        offset += reservedSize;
        
        [dict setObject:[NSNumber numberWithUnsignedLongLong:flags] forKey:flagsKey];
        [dict setObject:[NSString stringWithFormat:@"%llu.%llu", leftBalance, rightBalance] forKey:balanceKey];
    }
    
    return [dict copy];
}

@end
